package van.planifolia.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import van.planifolia.pojo.User;
import van.planifolia.service.UserService;
import van.planifolia.util.WebUtil;
import van.planifolia.vo.ComResult;
import van.planifolia.vo.UserVo;

import javax.annotation.Resource;

/**
 * Created by Intellij IDEA<br>
 * 用户的Controller层
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/10/10 14:44
 */
@RestController
@RequestMapping("/User")
public class UserController {

    /**
     * 引入用户的Service层
     */
    @Resource(name = "userService")
    private UserService userService;

    /**
     * 用户登录的Controller
     * @param loginVo 用户登录的vo对象
     * @return 公共的返回信息
     */
    @PostMapping("/Login")
    public ComResult login(@RequestBody UserVo loginVo){

        User user = userService.userLogin(loginVo.getUsername(), loginVo.getPassword());

        if (user != null) {
            WebUtil.getSession().setAttribute("loginInfo",user);
            return new ComResult(2000,"登录成功",user);
        }else {
            return new ComResult(5001,"登录失败", null);
        }
    }

    /**
     * 查询全部用户的Controller
     * @param pageIndex 分页下标
     * @param pageSize 分页大小
     * @param userVo 用户信息
     * @return 公共的返回结果
     */
    @PostMapping("/GetUsers")
    public ComResult getUsers(@RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "5") Integer pageSize, @RequestBody UserVo userVo){
        PageInfo<User> userList = userService.getUserList(pageIndex, pageSize, userVo);
        return new ComResult(2000,"查询成功",userList);
    }
    @PostMapping("/AddUser")
    public ComResult addUser(@RequestBody User user){
        int i = userService.addUserMsg(user);
        if (i>0){
            return new ComResult(2000,"插入成功");
        }else {
            return new ComResult(5000,"插入失败");
        }
    }

}
